﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GAM_Web_Designer_Pro.Properties;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace GAM_Web_Designer_Pro {
    public static class TemplateAccess {

        private static string _dataDir = Application.StartupPath + Settings.Default.TemplateDir;
        /// <summary>
        /// Loads a template from a file.
        /// </summary>
        /// <param name="filePath">The path to the template to load.</param>
        public static Template LoadTemplate( string filePath ) {
            Template template = new Template();
            try {
                FileStream stream = File.Open( filePath, FileMode.Open );
                BinaryFormatter formatter = new BinaryFormatter();

                template = (Template)formatter.Deserialize( stream );
                stream.Close();
                return template;
            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
            return template;
        }

        /// <summary>
        /// Loads all the template files in the template directory.
        /// </summary>
        public static SortedList<string, Template> LoadAllTemplates() {
            SortedList<string, Template> templates = new SortedList<string, Template>();
            try {
                FileStream stream;
                BinaryFormatter formatter = new BinaryFormatter();
                Template temp = new Template();
                string[] files = Directory.GetFiles( _dataDir );
                
                for ( int i = 0; i < files.Length; i++ ) {
                    stream = File.Open( files[ i ], FileMode.Open );
                    temp = (Template)formatter.Deserialize( stream );
                    templates.Add( temp.Key, temp );
                    stream.Close();
                }

                return templates;

            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
            return templates;
        }

        /// <summary>
        /// Saves a template to the template directory.
        /// </summary>
        /// <param name="template">Template to save</param>
        public static void SaveTemplate( Template template ) {
            try {

                FileStream stream = File.Create( _dataDir + @"\" + template.Name + Settings.Default.TemplateExt );
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize( stream, template );
                stream.Close();

            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
        }
    }
}
